package com.net.wanjian.networkhospitalmanager.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.bean.SearchRotateStudentInfoListResult;
import com.net.wanjian.networkhospitalmanager.utils.DateUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:
 * Created by lidongcai on 17/7/21.
 * Email:  lidongcaiocc@163.com
 */

public class RotateOrderExpandableAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<SearchRotateStudentInfoListResult.DataBean.RotationInfoBean> rotationInfo;

    public RotateOrderExpandableAdapter(Context context, List<SearchRotateStudentInfoListResult.DataBean.RotationInfoBean> rotationInfo) {
        this.context = context;
        this.rotationInfo = rotationInfo;
    }

    @Override
    public int getGroupCount() {
        return rotationInfo == null ? 0 : rotationInfo.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return rotationInfo.get(groupPosition).getRotationData() == null ? 0 : rotationInfo.get(groupPosition).getRotationData().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return rotationInfo.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return rotationInfo.get(groupPosition).getRotationData().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_rotate_order_expandle_group, parent, false);
            AutoUtils.auto(view);
            convertView = view;
            groupViewHolder = new GroupViewHolder(view);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        String date = URLDecoderUtil.getDecoder(rotationInfo.get(groupPosition).getRotationTime());
        String yearMonth = date.split("-")[0] + "-"+date.split("-")[1];
        groupViewHolder.itemRotateOrderGroupCalendarTv.setText(yearMonth);
        if (URLDecoderUtil.getDecoder(rotationInfo.get(groupPosition).getRotationAllDepartment()).equals("")){
            groupViewHolder.itemRotateOrderGroupDepartmentTv.setText(R.string.no_rotate_deparment);
        }else {
            String replace = URLDecoderUtil.getDecoder(rotationInfo.get(groupPosition).getRotationAllDepartment()).replace(",", "、");
            groupViewHolder.itemRotateOrderGroupDepartmentTv.setText(replace);
        }



        //得到当前的年月日
        String dateToStr = DateUtil.dateToStr(DateUtil.getDate());
        String currentYearMonth = dateToStr.split("-")[0] + "-"+dateToStr.split("-")[1];

        if (yearMonth.equals(currentYearMonth)) {
            //当月刷色
            groupViewHolder.itemRotateOrderGroupCalendarIv.setBackgroundResource(R.mipmap.calendar2);
            groupViewHolder.itemRotateOrderGroupCalendarTv.setTextColor(context.getResources().getColor(R.color.colorMain));
            groupViewHolder.itemRotateOrderGroupDepartmentTv.setTextColor(context.getResources().getColor(R.color.colorMain));
        } else {
            //默认样式
            groupViewHolder.itemRotateOrderGroupCalendarIv.setBackgroundResource(R.mipmap.calendar);
            groupViewHolder.itemRotateOrderGroupCalendarTv.setTextColor(context.getResources().getColor(R.color.color_item_text));
            groupViewHolder.itemRotateOrderGroupDepartmentTv.setTextColor(context.getResources().getColor(R.color.color_item_text));
        }


        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_rotate_order_expandle_child, parent, false);
            AutoUtils.auto(view);
            convertView = view;
            childViewHolder = new ChildViewHolder(view);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        SearchRotateStudentInfoListResult.DataBean.RotationInfoBean.RotationDataBean d = rotationInfo.get(groupPosition).getRotationData().get(childPosition);
        String startTime = URLDecoderUtil.getDecoder(d.getRotationStartTime()).split("-")[1] + "." + URLDecoderUtil.getDecoder(d.getRotationStartTime()).split("-")[2];
        String endTime = URLDecoderUtil.getDecoder(d.getRotationEndTime()).split("-")[1] + "." + URLDecoderUtil.getDecoder(d.getRotationEndTime()).split("-")[2];
        childViewHolder.itemRotateOrderChildDateTv.setText(startTime + " - " + endTime);
        if (URLDecoderUtil.getDecoder(d.getRotationTeacher().getUserInfoTrueName()).equals("")){
            childViewHolder.itemRotateOrderChildDepartmentDoctorTv.setText(URLDecoderUtil.getDecoder(d.getRotationDepartment()));
        }else {
            childViewHolder.itemRotateOrderChildDepartmentDoctorTv.setText(URLDecoderUtil.getDecoder(d.getRotationDepartment()) + "（"+URLDecoderUtil.getDecoder(d.getRotationTeacher().getUserInfoTrueName())+"）");
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    static class GroupViewHolder {
        @BindView(R.id.item_rotate_order_group_calendar_iv)
        ImageView itemRotateOrderGroupCalendarIv;
        @BindView(R.id.item_rotate_order_group_calendar_tv)
        TextView itemRotateOrderGroupCalendarTv;
        @BindView(R.id.item_rotate_order_group_department_tv)
        TextView itemRotateOrderGroupDepartmentTv;

        GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ChildViewHolder {
        @BindView(R.id.item_rotate_order_child_date_tv)
        TextView itemRotateOrderChildDateTv;
        @BindView(R.id.item_rotate_order_child_department_doctor_tv)
        TextView itemRotateOrderChildDepartmentDoctorTv;

        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
